package paf.importerandtranslators;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.*;


public class DiaFile implements FileStrategy{
	private Interpretation instructions = new Interpretation();;
	private String strExt;
	private String strDescription;
	
	public DiaFile(){
		this.strExt = "dia";
		this.strDescription = "Dia file";
	}
	
	public void setDiagramtype(File diafile) throws SAXException, IOException{
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse (diafile);
			
			document.getDocumentElement().normalize();
			System.out.println("Root "+ document.getDocumentElement().getNodeName());
			
			NodeList attributList = document.getElementsByTagName("dia:object");
			//instructions = new Interpretation();
			instructions.setDiagramType(attributList.item(0).getAttributes().getNamedItem("type").getNodeValue().trim());
		
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}

	@Override
	public Interpretation interpret(File diafile) throws SAXException, IOException {
		ArrayList<String> interpreted =  new ArrayList<String>();
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse (diafile);
			
			document.getDocumentElement().normalize();
			System.out.println("Root "+ document.getDocumentElement().getNodeName());
			
			NodeList attributList = document.getElementsByTagName("dia:string");
			int totalattributes = attributList.getLength();
			System.out.println("total attributes "+ totalattributes);
			
			for(int a = 0; a < attributList.getLength(); a++){
				Node naam = attributList.item(a);
				if(naam.getNodeType() == Node.ELEMENT_NODE){
					Element naamElement = (Element)naam;

					NodeList test = naamElement.getChildNodes();
					String klas = "";
					klas = test.item(0).getNodeValue().trim();
					klas = klas.replaceAll("#", " ");
						interpreted.add(klas);
						instructions.setInterpretedExpressions(interpreted);
						//for(int i = 0; i<instructions.getInterpretedExpressions().size(); i++){
							//System.out.println(" "+instructions.getInterpretedExpressions().get(i));
						//}
				}
				
			}
		
			//System.out.println("@klas " + interpreted);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setDiagramtype(diafile);
		return instructions;
	}

	@Override
	public String getExt() {
		return strExt;
	}
	
	@Override
	public String toString() {
		return strDescription + " (*." + strExt + ")";
	}

	@Override
	public String getDescription() {
		return strDescription;
	}
}
